package com.lajiaomian.springbootruijiwaimai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lajiaomian.springbootruijiwaimai.common.BaseContext;
import com.lajiaomian.springbootruijiwaimai.common.R;
import com.lajiaomian.springbootruijiwaimai.pojo.ShoppingCart;
import com.lajiaomian.springbootruijiwaimai.service.impl.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lajiaomian
 * @create 2022-08-17-22:22
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车：{}",shoppingCart);
        //给传来的数据设置userId属性
        shoppingCart.setUserId(BaseContext.getCurrId());
        //判断传过来的是dish还是setmeal
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrId());
        //如果dishId！=null说明传过来的是dish
        if(dishId!=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
            //如果==null就说明传过来的是setmeal
        }else {
            wrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }
        //查询对应的值，如果存在，就number+1，不存在就save
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(wrapper);
        if(shoppingCart1==null){
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shoppingCart1=shoppingCart;
        }else {
            Integer number = shoppingCart1.getNumber()+1;
            shoppingCart1.setNumber(number);
            shoppingCartService.updateById(shoppingCart1);
        }
        return R.success(shoppingCart1);
    }

    /**
     * 展示购物车项
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrId());
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> cleanCart(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrId());
        shoppingCartService.remove(wrapper);
        return R.success("清空成功");
    }

    /**
     * 减少商品项
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long UserId = BaseContext.getCurrId();
        shoppingCart.setUserId(UserId);
        //select * from shoppingCart where userId = ? and dishId=? and setmealid=?;
        ShoppingCart one = shoppingCartService.getOneByUserIdAndDishIdAndSetmealId(UserId, shoppingCart.getDishId(), shoppingCart.getSetmealId());
        Integer number = one.getNumber();
        if(number==1){
            shoppingCartService.removeById(one);
        }
        number=number-1;
        one.setNumber(number);
        shoppingCartService.updateById(one);
        return R.success(one);
    }
}
